package com.viethp.mt.activities;

import java.util.ArrayList;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import com.viethp.mt.adapters.OItemAdapter;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.GlobalVariables;
import com.viethp.mt.objects.OItem;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AAdvancedSearch extends Activity implements OnClickListener{
	
	private ListView lv_items;
	private ArrayList<OItem> list_Oitems;
	private OItemAdapter listoitemadapter;
	private int countDemo, num;
	
	private ProgressDialog progressDialog;
	
	public View footerView;
	private int totaPages;
	private int pages;
	private TextView txtshowmore, tv_result, tv_category, tv_distance, tv_price_range;
	private ImageView img_search_category, img_search_distance, img_search_price_range;
	private Button btn_search, btn_back;
	
	private String[] category_list_array=new String[]{"All","Mobile phone","Household electrical appliances","Computer","Telecommunications equipment"};
	private String[] distance_list_array=new String[]{"All","1 mile","3 miles","7 miles","10 miles"};
	private String[] price_list_array=new String[]{"All","< $10","$10 - $20","$20 - $50","$50 - $100","$100 - $500","> $500"};
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.advanced_search);
		
		initComponents();
		setOnclickListener();
	}
	
	public void initComponents(){
		lv_items = (ListView) findViewById(R.id.lv_item_search);
		list_Oitems = new ArrayList<OItem>();
		
		footerView = ((LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.listfooter, null, false);
		lv_items.addFooterView(footerView);
		footerView.setVisibility(View.GONE);
		
		footerView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (pages < totaPages) {
					pages++;
					initData();

				}

			}
		});
		
		listoitemadapter = new OItemAdapter(this, R.layout.lv_items, list_Oitems);
		lv_items.setAdapter(listoitemadapter);
		
		//add
		txtshowmore = (TextView) findViewById(R.id.idshow);
		
		progressDialog = new ProgressDialog(AAdvancedSearch.this);
		progressDialog.setCancelable(false);
		
		totaPages = 1;
		pages = 1;
		countDemo=1;
//		initData();
		
		btn_search = (Button) findViewById(R.id.btn_search);
		btn_back = (Button) findViewById(R.id.btn_back);
		tv_result = (TextView) findViewById(R.id.tv_result);
		
		tv_category = (TextView) findViewById(R.id.tv_category);
		tv_distance = (TextView) findViewById(R.id.tv_distance);
		tv_price_range = (TextView) findViewById(R.id.tv_price_range);
		
		img_search_category = (ImageView) findViewById(R.id.img_search_category);
		img_search_distance = (ImageView) findViewById(R.id.img_search_distance);
		img_search_price_range = (ImageView) findViewById(R.id.img_search_price_range);
	}
	
	public void setOnclickListener(){
		btn_search.setOnClickListener(this);
		btn_back.setOnClickListener(this);
		
		img_search_category.setOnClickListener(this);
		img_search_distance.setOnClickListener(this);
		img_search_price_range.setOnClickListener(this);
	}

	public void demoData(int number){
		if(countDemo==1){
			num=number;
			for(int i=0; i<num; i++){
				double temp = (double) i/1000;
				OItem oi = new OItem(i, i,"username"+i ,"title "+i, "description " + i, i, i, 108.219473 + temp, 16.085444 + temp, 1336876802, 1336876810);
				list_Oitems.add(oi);
			}
		}
		else {
			for(int i=num; i<(num=number*countDemo); i++){
				double temp = (double) i/1000;
				OItem oi = new OItem(i, i,"username"+i ,"title "+i, "description " + i, i, i, 108.219473 + temp, 16.085444 + temp, 1336876802, 1336876810);
				list_Oitems.add(oi);
			}
		}
		countDemo++;
	}
	
	public void showResult(){
		if(list_Oitems.size()==0){
			tv_result.setVisibility(View.GONE);
		} else {
			tv_result.setVisibility(View.INVISIBLE);
		}
	}
	
	private void startCallService() {
		
		progressDialog.setMessage(getString(R.string.pleasewait));
		progressDialog.show();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Message message = new Message();
				message.what = callService();
				handler.sendMessage(message);
				
			}
		}).start();
		
	}
	
	public void initData() {

		if (CommonHelper.isNetworkAvailable(this)) {
			startCallService();
		} else {
			CommonHelper.showWarning(this, getString(R.string.netstate));
		}

	}
	private int callService() {

		demoData(10);
		totaPages=5;
		return GlobalVariables.ReOK;

	}
//
	private Handler handler = new Handler() {
			
			public void handleMessage(Message msg) {
				progressDialog.dismiss();
				
				if(msg.what == GlobalVariables.ReOK){
					if (pages == totaPages) {

						if (lv_items.getFooterViewsCount() != 0)
							lv_items.removeFooterView(footerView);
						lv_items.setFooterDividersEnabled(true);

					} else {
						footerView.setVisibility(View.VISIBLE);
						lv_items.setFooterDividersEnabled(true);
						if (lv_items.getFooterViewsCount() == 0)
							lv_items.addFooterView(footerView);
					}

					txtshowmore.setText("Showing " + pages + " of " + totaPages
							+ " pages");
					listoitemadapter.notifyDataSetChanged();
				}else {
					CommonHelper.showWarning(getParent(),
							getString(R.string.loaddatastate));
	
				}
	
			};
		};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			pages=1;
			countDemo=1;
			list_Oitems.removeAll(list_Oitems);
			initData();
			showResult();
			break;
		case R.id.img_search_category:
			DialogCategory();
			break;
		case R.id.img_search_distance:
			DialogDistance();
			break;
		case R.id.img_search_price_range:
			DialogPriceRange();
			break;
		case R.id.btn_back:
			this.finish();
		default:
			break;
		}
		
	}
	
	//Dialog
	
	private WheelView initWheel(int id, String[] values, View view) {
		WheelView wheel = (WheelView) view.findViewById(id);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(this, values);
		adapter.setTextSize(20);
		wheel.setViewAdapter(adapter);
		wheel.setCurrentItem(1);
		wheel.setCyclic(true);
		wheel.setInterpolator(new AnticipateOvershootInterpolator());
		
		return wheel;
	}
	
	public void DialogCategory(){
		LayoutInflater layoutInflater 
		  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view=layoutInflater.inflate(R.layout.dialog_wheel,null);
		final WheelView category = initWheel(R.id.wheel_dialog,category_list_array,view);
		
		new android.app.AlertDialog.Builder(this).setTitle("Select category").setView(view).setPositiveButton("OK", new DialogInterface.OnClickListener(){
			   @Override
			   public void onClick(DialogInterface arg0, int arg1) {
				 
				   int cate = category.getCurrentItem();
				   tv_category.setText(category_list_array[cate]);
			     
	   }}).setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
			public void onClick(
					android.content.DialogInterface dialog,
					int whichButton) {

			}
		}).create().show();
	}
	
	public void DialogDistance(){
		LayoutInflater layoutInflater 
		  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view=layoutInflater.inflate(R.layout.dialog_wheel,null);
		final WheelView category = initWheel(R.id.wheel_dialog,distance_list_array,view);
		
		new android.app.AlertDialog.Builder(this).setTitle("Select distance").setView(view).setPositiveButton("OK", new DialogInterface.OnClickListener(){
			   @Override
			   public void onClick(DialogInterface arg0, int arg1) {
				 
				   int cate = category.getCurrentItem();
				   tv_distance.setText(distance_list_array[cate]);
			     
	   }}).setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
			public void onClick(
					android.content.DialogInterface dialog,
					int whichButton) {

			}
		}).create().show();
	}
	
	public void DialogPriceRange(){
		LayoutInflater layoutInflater 
		  = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View view=layoutInflater.inflate(R.layout.dialog_wheel,null);
		final WheelView category = initWheel(R.id.wheel_dialog,price_list_array,view);
		
		new android.app.AlertDialog.Builder(this).setTitle("Select price range").setView(view).setPositiveButton("OK", new DialogInterface.OnClickListener(){
			   @Override
			   public void onClick(DialogInterface arg0, int arg1) {
				 
				   int cate = category.getCurrentItem();
				   tv_price_range.setText(price_list_array[cate]);
			     
	   }}).setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
			public void onClick(
					android.content.DialogInterface dialog,
					int whichButton) {

			}
		}).create().show();
	}
}
